#include"GFriendsList.h"

//**************************************************
//GFriend
//**************************************************
GFriend::GFriend(QObject* parent, QString str, int lastindex) : QObject(parent) {
	ind = lastindex;
	nam = str;
	initstat = true;
}

GFriend::~GFriend() {
	disconnect(this, SLOT(receiveStatusUpdate(QString)));
}

void GFriend::name(QString str) {
	nam = str;
}

QString GFriend::name() {
	return nam;
}

int GFriend::index() {
	return ind;
}

void GFriend::index(int i) {
	ind = i;
}

QString GFriend::status() {
	return stat;
}

void GFriend::status(QString str) {
	stat = str;
}

void GFriend::receiveStatusUpdate(QString str) {
	stat = str;
	cout<<"name: " <<nam.toStdString() <<" stat: " <<str.toStdString() <<endl;
	if(initstat) initstat = false;
	else emit sendStatusUpdate(nam, str);
}



//**************************************************
//GFriendsList
//**************************************************
GFriendsList::GFriendsList(QObject* parent) : QObject(parent) {
	c = false;
	
	
}

GFriend* GFriendsList::operator[](int i) {
	return l[i];
}

void GFriendsList::save(QString fn) {
	if(!changed()) return;
	if(QFile::exists(fn)) 
		QFile::remove(fn);
	
	QFile file(fn);
	if(file.open(QIODevice::ReadWrite)) {
		file.seek(0);
		QTextStream stream(&file);
		stream.seek(0);
		int length = l.size();
		for(int i=0; i<length; i++) {
			stream<<l[i]->name() <<"," <<l[i]->index() <<"\n";
		}
		c = false;
	}
}

void GFriendsList::load(QString fn) {
	QFile file(fn);
	if(file.open(QIODevice::ReadOnly)) {
		QTextStream stream(&file);
		QString temp;
		while(!(temp = stream.readLine(0)).isEmpty()) {
			QStringList args = temp.split(tr(","), QString::SkipEmptyParts);
			bool ok = false;
			int lastindex = -1;
			if(args.size() > 1) {
				lastindex = args[1].toInt(&ok);
			}
			GFriend* tmp;
			if(ok) tmp = new GFriend(this, args[0], lastindex);
			else tmp = new GFriend(this, args[0], -1);
			connect(tmp, SIGNAL(sendStatusUpdate(QString,QString)), 
							this, SLOT(forwardStatus(QString,QString)));
			
			l.push_back(tmp);
		}
	}
	for(int i=0; i<size(); i++) {
	}
}

GFriend* GFriendsList::add(QString name) {
	int length = l.size();
	for(int i=0; i<length; i++) {
		if(l[i]->name() == name) return 0;
	}
	c = true;
	GFriend* tmp = new GFriend(this, name, -1);
	connect(tmp, SIGNAL(sendStatusUpdate(QString,QString)), this, 
					SLOT(forwardStatus(QString,QString)));
	l.push_back(tmp);
	return tmp;
}

void GFriendsList::remove(QString name) {
	int length = l.size();
	for(int i = 0; i<length; i++) {
		if(l[i]->name() == name) {
			delete l[i];
			l[i] = 0;
			l.removeAt(i);
			c = true;
			return;
		}
	}
}

void GFriendsList::clear() {
	int length = l.size();
	for(int i=0; i<length; i++) {
		GFriend* tmp = l[i];
		l[i] = 0;
		delete tmp;
		tmp = 0;
	}
	l.clear();
	c = false;
}

QString GFriendsList::listCSV() {
	int length = l.size();
	QString retlist;
	for(int i=0; i<length; i++) {
		retlist.append(l[i]->name());
		retlist.append(tr("`"));
		retlist.append(l[i]->status());
		retlist.append("`");
	}
	return retlist;
}

bool GFriendsList::changed() {
	return c;
}

void GFriendsList::setChanged() {
	c = true;
}

int GFriendsList::size() {
	return l.size();
}

void GFriendsList::forwardStatus(QString name, QString status) {
	emit forwardStatusSig(name, status);
}
